package tech.abing.spider.task.pageprocessor;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import tech.abing.spider.component.PageProcessor;
import tech.abing.spider.component.PageScript;
import tech.abing.spider.model.Proccessable;
import tech.abing.spider.page.OkPage;
import tech.abing.spider.page.Page;
import tech.abing.spider.request.BasicRequest;
import tech.abing.spider.request.PageRequest;
import tech.abing.spider.request.PageRequest.PageEncoding;
import tech.abing.spider.request.StartContext;
import tech.abing.spider.util.StringUtils;

public class PayLogProccessor1 implements PageProcessor {

	static Pattern normalContain = Pattern.compile("淘宝");
	
	static AtomicInteger atomicInteger = new AtomicInteger(0);//记录访问次数
	
	@Override
	public PageScript getJavaScript() {
		return null;
	}

	@Override
	public Pattern getNormalContain() {
		return normalContain;
	}

	@Override
	public void process(OkPage page, StartContext context, List<BasicRequest> queue, List<Proccessable> objectContainer)
			throws Exception {
		
		 Document doc = Jsoup.parse(page.getContent());
		 String type =  page.getRequest().getParamsByName("type").toString();
		 String distUrl = "";
		 String temp = "";
		 Elements eles = null;
		 if("TMALL".equals(type)){
			 distUrl = doc.select("button[id=J_listBuyerOnView]").attr("detail:params");
		 }else{
			eles = doc.select("script");
			
			for(Element x:eles){
				if(x.html().contains("g_config.recordsApi=")){
					temp = x.html();
					temp = temp.substring(temp.indexOf("recordsApi=")+12);
					temp = temp.substring(0,temp.indexOf("\""));
					break;
				}
			}
			distUrl = temp;
		 }
		 if(StringUtils.isNotBlank(distUrl)){
			 distUrl = "https:"+distUrl;
			 PageRequest req = null;
			 context.putContextAttribute("atomicInteger", atomicInteger);
			 if("TMALL".equals(type)){
				 distUrl =distUrl.replace(",showBuyerList", "")+"&callback=jsoup";
				 req = context.createPageRequest(distUrl, PayLogProccessor2.class, 1, PageEncoding.UTF8);
			 }else{
				 distUrl = distUrl+"&bid_page=1";
				 req = context.createPageRequest(distUrl, PayLogProccessor21.class, 1, PageEncoding.UTF8);
			 }
			 String itemId = page.getRequest().getParamsByName("itemId").toString();
			 req.putParams("itemId", itemId);
			 req.putParams("shopId", page.getRequest().getParamsByName("shopId").toString());
			 req.putParams("type", type);
			 req.putParams("catchDate", page.getRequest().getParamsByName("catchDate").toString());
			 req.putParams("lastDate", page.getRequest().getParamsByName("lastDate").toString());
			 req.putParams("monthSale", page.getRequest().getParamsByName("monthSale").toString());
			 
			 //req.putParams("cookie", SysUtil.getValue("trade.cookie"));
			 //req.putParams("cookie", StringUtils.getRandomCookie());
			 req.putParams("cookie", StringUtils.toString(context.getContextAttribute("cookie")));
			 req.putParams("referer", "https://item.taobao.com/item.htm?id="+itemId);
			 
			 req.setUseJsoup(true);
			 queue.add(req);//跟进队列
		 }

	}


	@Override
	public void processErrorPage(Page page, StartContext context,
			List<Proccessable> objectContainer) throws Exception {
		// TODO Auto-generated method stub
		
	}

}
